---
--类名: PriceInfoView.lua
--创建日期: 2018-8-31
--创建人: 宋建邦
--功能描述:竞价信息视图
--修改内容:
--修改日期:
--修改人:
--修改备注:

local PriceInputView = import(".PriceInputView")
local PriceBidTipsPanel = import(".PriceBidTipsPanel")

local PriceInfoView = class("PriceInfoView", app.mvc.ViewBase)
PriceInfoView.VIEW_ROOT_RES = Res.PriceInfoView

function PriceInfoView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)

    self.mapName = self._root:findChild("Panel/tlt_name")
    self.mapIcon = self._root:findChild("Panel/map")
    self.teamInfoString = self._root:findChild("Panel/tlt_team_name")
    self.prosperityString = self._root:findChild("Panel/tlt_boom_num")

    --显示可以竞价的panel
    self._canBidPanel = self._ui.Panel_1
    --显示不可以竞价的Panel
    self._canNotBidPanel = self._ui.Panel_2


    self.guildMoney = self._canBidPanel:findChild("Panel_num_2/num")
    self.curPrice = self._canBidPanel:findChild("Panel_num_1/num")
    self.goldIcon1 = self._canBidPanel:findChild("Panel_num_1/gold")
    self.goldIcon2 = self._canBidPanel:findChild("Panel_num_2/gold")

    self.openDateLabel = self._canBidPanel:findChild("tips_1")

    for i = 1, 2 do
        self["desc"..tostring(i)] = self._root:findChild("Panel/Panel_text_"..tostring(i).."/tlt")
        self["num"..tostring(i)] = self._root:findChild("Panel/Panel_text_"..tostring(i).."/num")
    end

    self.closeBtn = self._root:findChild("Panel/Panel_1/Button_close")
    self.closeBtn:findChild("name"):setString(L("lua_code_text_362"))
    self.closeBtn:addClickEventListener(function()
        -- local bc = app:getInst("BiddingController")
        -- bc:closePriceView()
        local piv = PriceInputView.new()
        piv:refresh(self._data)
        piv:show()
        UIHandler:addPopupUi(piv, piv:getViewName())
    end)

    self.bc = app:getInst("BiddingController")
    display.nodeRemoveForNotHit(self._root:findChild("Panel"), function()
        self.bc:closePriceView()
    end)

    self.evthandler = EM:on(self.bc.townlistChange, function()
        if self._data and self.bc.battleMapModel.hTownInfos[self._data.data.id] then
            self:refresh(self.bc.battleMapModel.hTownInfos[self._data.data.id])
        end
    end)

    --打开这个界面后有两种情况 根据服务器开放时间获取当前统治力繁荣度上限 若超过了 则显示self._canNotBidPanel 否则显示self._canBidPanel
    local openServerDays = app.session:getMerageOpenDays() -- 开服第几天
    local forceLimitInfo = GD:getForceLimitInfoByDay(openServerDays)
    local guildModel = app:getInst("NewguildModel")
    local myGuildInfo = guildModel:getMyGuildInfo()
    --print(openServerDays,checknumber(forceLimitInfo.limitnum),myGuildInfo:getMaxProsperity())

    if myGuildInfo and forceLimitInfo and (checknumber(forceLimitInfo.limitnum) == 0 or checknumber(forceLimitInfo.limitnum) > myGuildInfo:getMaxProsperity()) then
        self._canBidPanel:setVisible(true)
        self._canNotBidPanel:setVisible(false)
    else
        self._canBidPanel:setVisible(false)
        self._canNotBidPanel:setVisible(true)

        self._canNotBidPanel:findChild("tips_1"):setString(string.format(L("guildwarlimit_string1"),myGuildInfo:getMaxProsperity()))
        self._canNotBidPanel:findChild("tips_2"):setString(L("guildwarlimit_string2"))
        self._canNotBidPanel:findChild("tips_3"):setString(string.format(L("guildwarlimit_string3"),openServerDays,checknumber(forceLimitInfo.limitnum)))
        self._canNotBidPanel:findChild("btn_more/name"):setString(L("guildwarlimit_string4"))
        self._canNotBidPanel:findChild("btn_more"):addClickEventListener(function()
            --准备显示一个tips界面
            self:showTips()

        end)
    end
end

function PriceInfoView:showTips()
    if cc.isDead( self._guildTips ) then
        self._guildTips = PriceBidTipsPanel.create()

        self._ui.tips:addChild( self._guildTips )
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._guildTips, function()
            if self._guildTips then
                self._guildTips:removeFromParent(true)
                self._guildTips = nil
            end
        end)
    end

    self._guildTips:setVisible( true )
    self._guildTips:setPosition(cc.p(0,0))
end

function PriceInfoView:onExit()
    EM:remove(self.evthandler)
end

function PriceInfoView:refresh(data)
    -- dump(data, "town info ==>")
    local function centerNodes(nodes)
        local w1 = nodes[1]:getContentSize().width
        local w2 = nodes[2]:getContentSize().width
        local pw = nodes[1]:getParent():getContentSize().width

        local ow = (pw - (w1 + w2)) * 0.5

        nodes[1]:setPositionX(ow + w1 * 0.5)
        nodes[2]:setPositionX(ow + w1 + w2 * 0.5)
    end
    self._data = data
    if self._data then
        local cfg = self._data.config
        local maxProsperity = 0
        if cfg then
            self.mapName:setString(cfg:getName())
            self.mapIcon:loadTexture(cfg:getIcon())
            maxProsperity = cfg.blessExpMax

            local nums = {
                L("+")..tostring(cfg._conf.temporaryNumber),
                L("+")..tostring(cfg._conf.temporaryFame),
            }

            for i = 1, 2 do
                self["num"..tostring(i)]:setString(tostring(nums[i]))
            end

            local biddendtime = GD:getSystemStrPar("GuildWarBiddingEndTime")
            local ps = string.split(biddendtime, "|")
            local tstr = string.format("%.2d", checknumber(ps[1]))..":"..string.format("%.2d", checknumber(ps[2]))

            local daystr = self._data:getStartWeekDay()
            self.openDateLabel:setString(string.format(L("lua_code_text_211"), daystr, tstr))

        end

        local data = self._data.data
        if data then
            local teamStr = self._data:getGuildName()
            if teamStr then
                teamStr = string.format(L("lua_code_text_469"), teamStr) --L(teamStr)..L("军团领地")
            else 
                teamStr = L("lua_code_text_172")
            end
            self.teamInfoString:setString(teamStr)
            self.prosperityString:setString(string.format(L("lua_code_text_229"), data.prosperity, maxProsperity))

            self.curPrice:setString(string.format(L("lua_code_text_39"), data.myBiddingPrice))
            centerNodes({self.curPrice, self.goldIcon1})
        end
        
        local strs = {
            L("lua_code_text_56"),
            L("lua_code_text_66"),
            L("lua_code_text_78")
        }
        for i = 1, 2 do
            self["desc"..tostring(i)]:setString(strs[i])
        end

        local gm = app:getInst("NewguildModel")
        local ginfo = gm:getMyGuildInfo()
        local guildMoney = 0
        local needGuildLv = tonumber(cfg._conf.auctionNeedLv)
        if ginfo then
            guildMoney = ginfo:getGuildFund()

            --公会等级若没达到要求不能竞价 隐藏按钮
            if ginfo:getGuildLv() < needGuildLv then
                self._canBidPanel:findChild("tips_3"):setVisible(true)
                self._canBidPanel:findChild("tips_2"):setVisible(true)
                self._canBidPanel:findChild("tips_3"):setString(string.format(L("GuildWarGuildLvdesc"),ginfo:getGuildLv()))
                self._canBidPanel:findChild("tips_2"):setString(string.format(L("GuildWarBiddingGuildLvdesc"),needGuildLv))
                self.closeBtn:setVisible(false)
                self._canBidPanel:findChild("Panel_num_2"):setVisible(false)
            else
                self._canBidPanel:findChild("tips_3"):setVisible(false)
                self._canBidPanel:findChild("tips_2"):setVisible(false)
                self.closeBtn:setVisible(true)
                self._canBidPanel:findChild("Panel_num_2"):setVisible(true)
            end
        else
            --没工会就别想竞价了
            self.closeBtn:setVisible(false)
            self._canBidPanel:findChild("tips_2"):setVisible(true)
            self._canBidPanel:findChild("tips_2"):setString(string.format(L("GuildWarBiddingGuildLvdesc"),needGuildLv))
            self._canBidPanel:findChild("Panel_num_2"):setVisible(false)
        end
        self.guildMoney:setString(string.format(L("lua_code_text_1"), guildMoney))
        centerNodes({self.guildMoney, self.goldIcon2})
    end
end

return PriceInfoView